﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace SAP.BusinessOne.Cockpit
{
    public interface IContext
    {
        string Company { get; }
        string User { get; }
        string Locale { get; }
        string TenantConnection { get; }
    }

    class InteropContext : IContext
    {
        private const int NCP_SCHEMA = 1;
        private const int NCP_USER = 2;
        private const int NCP_LOCALE = 3;
        private const int NCP_CONNECTION = 4;
        private const int MAX_CHAR = 256;

        [DllImport("BusinessOne.exe", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        private static extern int QueryEnv(int kind, StringBuilder buffer, int length);

        public string Company
        {
            get
            {
                StringBuilder buffer = new StringBuilder(MAX_CHAR);
                QueryEnv(NCP_SCHEMA, buffer, MAX_CHAR);
                return buffer.ToString();
            }
        }

        public string User
        {
            get
            {
                StringBuilder buffer = new StringBuilder(MAX_CHAR);
                QueryEnv(NCP_USER, buffer, MAX_CHAR);
                return buffer.ToString();
            }
        }

        public string Locale
        {
            get
            {
                StringBuilder buffer = new StringBuilder(MAX_CHAR);
                QueryEnv(NCP_LOCALE, buffer, MAX_CHAR);
                return buffer.ToString();
            }
        }

        public string TenantConnection
        {
            get
            {
                StringBuilder buffer = new StringBuilder(MAX_CHAR);
                QueryEnv(NCP_CONNECTION, buffer, MAX_CHAR);
                return buffer.ToString();
            }
        }

    }
}
